package com.im.app.controller;

import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.model.Admin;
import com.im.app.model.Code;
import com.im.app.model.FlowRecordLogs;
import com.im.app.model.User;
import com.im.app.service.*;
import com.im.app.utils.*;
import org.jim.core.cache.redis.JedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import java.util.Calendar;
import java.util.Date;


/**
 * 续费
 *
 * @author aaa
 * @date 2023-04-12
 */
@RestController
@RequestMapping(value = "/reCharge")
public class RechargeController {

    @Resource
    private IUserService userService;
    @Resource
    private ICodeService codeService;
    @Resource
    private IPersonhostService personhostService;
    @Resource
    private IAdminService adminService;
    @Resource
    private IFlowRecordLogsService flowRecordService;

    @GetMapping("/xuFei")
    public ResponseResult<Object> xuFei(String token, String dlKami, String xfKami, Integer type){
        if (xfKami == null){
            return Response.makeErrRsp("参数错误");
        }
        if (type == null){
            return Response.makeErrRsp("参数错误");
        }
        User user = null;
        if(type == 1){
            if(token!=null){
                try {
                    user = JedisTemplate.me().get(token, User.class);
                } catch (Exception e) {
                    return Response.makeErrRsp("获取登录用户失效,请重新登录");
                }
            }
        }else if (type == 2){
            if (dlKami == null){
                return Response.makeErrRsp("参数错误");
            }
            user = userService.getUserBykamiCode(dlKami);
        }else {
            return Response.makeErrRsp("参数错误");
        }
        if(null==user) {
            return Response.makeErrRsp("续费用户不存在");
        }
        Code code = codeService.getCodeByKamicode(xfKami);
        if(code == null){
            return Response.makeErrRsp("续费卡密有误");
        }else if(!"usexufei".equals(code.getKamitype())){
            return Response.makeErrRsp("卡密类型有误");
        }else if(Constants.KAMIINUSE.equals(code.getKamistatus())){
            return Response.makeErrRsp("续费卡密已使用过");
        }
        Date endTime = user.getEndTime();
        Date nowTime = new Date();
        Date startDate;
        if (endTime.getTime() < nowTime.getTime()) {
            startDate = nowTime;
        } else {
            startDate = endTime;
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(startDate);
        rightNow.add(Calendar.DATE, code.getDays());
        user.setEndTime(rightNow.getTime());
        boolean update = userService.updateById(user);
        if(update){
            code.setKamistatus(Constants.KAMIINUSE);
            code.setUsetime(new Date());
            code.setEndTime(rightNow.getTime());
            code.setUserId(user.getKamicode());
            codeService.updateById(code);
            if (code.getType() > 0 && code.getAdminId() != null &&  code.getAdminId() > 0){
                Admin admin = adminService.selectAdminById(code.getAdminId());
                FlowRecordLogs flowRecord = FlowRecordLogs.createRecord(admin,FlowRecordLogs.USE,code.getKamicode() + "续费卡密已使用");
                flowRecordService.insertFlowRecordLogs(flowRecord);
            }

            //是否需要重新分配域名
            //personhostService.updateUsersByOpration(user);
            //更新token
            try {
                JedisTemplate.me().set(token, user,60*60*24);
            } catch (Exception e) {
            }
        }else {
            return Response.makeErrRsp("操作失败！");
        }
        return Response.makeOKRsp("续费成功！").setData(user);
    }

    @GetMapping("/singleBuy")
    @RequireLogin
    public ResponseResult<Object> singleBuy(@CurrentUser User user,String kamiCode){
        if (kamiCode == null){
            return Response.makeErrRsp("独立卡密不能为空");
        }
        Code code = codeService.getCodeByKamicode(kamiCode);
        if(code == null){
            return Response.makeErrRsp("独立卡密有误");
        }else if(!"useduli".equals(code.getKamitype())){
            return Response.makeErrRsp("卡密类型有误");
        }else if(Constants.KAMIINUSE.equals(code.getKamistatus())){
            return Response.makeErrRsp("独立卡密已使用过");
        }

        //是否需要重新分配域名
        personhostService.updateUsersByDlOpration(user,kamiCode);
        code.setKamistatus(Constants.KAMIINUSE);
        code.setUsetime(new Date());
        code.setUserId(user.getKamicode());
        codeService.updateById(code);

        if (code.getType() > 0 && code.getAdminId() != null &&  code.getAdminId() > 0){
            Admin admin = adminService.selectAdminById(code.getAdminId());
            FlowRecordLogs flowRecord = FlowRecordLogs.createRecord(admin,FlowRecordLogs.USE,code.getKamicode() + "购买独立域名已使用");
            flowRecordService.insertFlowRecordLogs(flowRecord);
        }
        return Response.makeOKRsp("操作成功！");
    }

}
